package stack_queue;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description: 20. 有效的括号
 * User: Li_yizYa
 * Date: 2025/5/18
 * Time: 17:21
 */
public class Solution3 {
    private static boolean isValid(String s) {
        if (s == null || s.length() == 0) {
            return false;
        }
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '(' || c == '{' || c == '[') {
                stack.push(c);
            } else {
                if (stack.isEmpty()) {
                    return false;
                }
                char tmp = stack.pop();
                if (c == ')' && tmp == '(' || c == ']' && tmp == '[' || c == '}' && tmp == '{') {
                    continue;
                } else {
                    return false;
                }
            }
        }
        if (!stack.isEmpty()) {
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(isValid("["));
        System.out.println(isValid("[](}"));
        System.out.println(isValid("(){}[]"));
    }
}
